//index.js
const app = getApp()
let user=require('../../lib/user');
Page({
  mixins: [require('../../lib/themeChanged'),require('../../lib/openId')],
  data: {
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') // 如需尝试获取用户信息可改为false
  },
  onLoad: function() {
    if (!wx.cloud) {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
      return
    }
    if (!wx.getUserProfile) {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
    wx.navigateTo({
      url: '/pages/edit/addDetail?_id='+'28ee4e3e60cc70a623528e8f07414b25',
    })
  },

  async getUserProfile() {
    const  userInfo=await user.userInfo('提供个性化服务');
    if (userInfo==undefined){
      wx.showModal({
        title: '极宣推广',
        content:'授权后才能管理，已经制作的应用',
        showCancel:false
      })
      return;
    }
    wx.navigateTo({
      url: '/pages/show/myList',
    })
  },
  // 上传图片
  doUpload: function () {
    // 选择图片
    wx.chooseImage({
      count: 1,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        wx.showLoading({
          title: '上传中',
        })

        const filePath = res.tempFilePaths[0]
        
        // 上传图片
        const cloudPath = `my-image${filePath.match(/\.[^.]+?$/)[0]}`
        wx.cloud.uploadFile({
          cloudPath,
          filePath,
          success: res => {
            console.log('[上传文件] 成功：', res)

            app.globalData.fileID = res.fileID
            app.globalData.cloudPath = cloudPath
            app.globalData.imagePath = filePath
            
            wx.navigateTo({
              url: '../storageConsole/storageConsole'
            })
          },
          fail: e => {
            console.error('[上传文件] 失败：', e)
            wx.showToast({
              icon: 'none',
              title: '上传失败',
            })
          },
          complete: () => {
            wx.hideLoading()
          }
        })
      },
      fail: e => {
        console.error(e)
      }
    })
  },
  create:()=>{
    wx.navigateTo({
      url: '/pages/edit/create',
    })
  }
})
